Overloading functions in the global context

Hi

I have noticed a problem when overloading functions which exist in the global context. It seems like the references to the global functions are messed up, see example below. Does anyone have an explanation to this behavior?

// Overloading existing 'fileExists_' (other return value and parameters)
string fileExists_(string sFile, bool &b)
{
    return("")
}
 
// Try using original 'fileExists_'
bool b = fileExists_("hello")
print b


/Anders


a_vestlin - Mon May 10 06:54:43 EDT 2010

Re: Overloading functions in the global context
a_vestlin - Mon May 10 07:01:37 EDT 2010

Maybe I should add that I get the following error message:

-E- DXL: <Line:8> incorrect use of identifier (fileExists_)
-I- DXL: all done with 1 error and 0 warnings

Re: Overloading functions in the global context
kbmurphy - Mon May 10 12:51:06 EDT 2010

a_vestlin - Mon May 10 07:01:37 EDT 2010

Maybe I should add that I get the following error message:

-E- DXL: <Line:8> incorrect use of identifier (fileExists_)
-I- DXL: all done with 1 error and 0 warnings

Because you aren't using the overloaded function properly?
 

string fileExists_(string sFile, bool &b)
 
{
    b = true
        return("")
}
 
// Try using original 'fileExists_'
bool b 
fileExists_("hello", b)
print b

Re: Overloading functions in the global context
a_vestlin - Tue May 11 02:24:22 EDT 2010

kbmurphy - Mon May 10 12:51:06 EDT 2010

Because you aren't using the overloaded function properly?
 

string fileExists_(string sFile, bool &b)
 
{
    b = true
        return("")
}
 
// Try using original 'fileExists_'
bool b 
fileExists_("hello", b)
print b

No, my point is that I cannot use the original function (if it is declared in the global context) after
a have declared another one with the same name.

This would work:

// Redefine original 'fileExists_'
bool fileExists_(string sFile)
{
    Stat st
        bool b
        st = create(sFile)
        if ( !null(st) )
        {
                delete(st)
                return(true)
        }
        return(false)
}
 
string fileExists_(string sFile, bool &b)
 
{
        b = true
        return("")
}
 
// Try using redefined original 'fileExists_'
bool b 
b = fileExists_("hello")
print b

 


And this also works:

 

 

// Try using original 'fileExists_' without any overloading
bool b 
b = fileExists_("hello")
print b



But the code in my original post does not work.

/Anders



 

Re: Overloading functions in the global context
Mathias Mamsch - Tue May 11 10:40:38 EDT 2010

a_vestlin - Tue May 11 02:24:22 EDT 2010

No, my point is that I cannot use the original function (if it is declared in the global context) after
a have declared another one with the same name.

This would work:

// Redefine original 'fileExists_'
bool fileExists_(string sFile)
{
    Stat st
        bool b
        st = create(sFile)
        if ( !null(st) )
        {
                delete(st)
                return(true)
        }
        return(false)
}
 
string fileExists_(string sFile, bool &b)
 
{
        b = true
        return("")
}
 
// Try using redefined original 'fileExists_'
bool b 
b = fileExists_("hello")
print b

 


And this also works:

 

 

// Try using original 'fileExists_' without any overloading
bool b 
b = fileExists_("hello")
print b



But the code in my original post does not work.

/Anders



 

So obviously the parser will lookup symbols in the current context first and not search the top level context when it finds the symbol. Thats bad. Anyway you can get around it, by hooking the function in the current context before redeclaring it:
 

// "hook" the function in the current context before overriding it ...
bool fileExistsOld (string file) { return fileExists_ file }
// override it 
bool fileExists_ (string file, bool b) { /* some code */ }
 
string s = "c:\\test.dxl"
 
print (fileExistsOld s) "\n"
print (fileExists_ (s, true)) "\n"

 


Regards, Mathias

 

 

 


Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS